package com.feiclub.subject.domain.handle.subject;


import com.feiclub.subject.common.enums.IsDeletedFlagEnum;
import com.feiclub.subject.common.enums.SubjectInfoTypeEnum;
import com.feiclub.subject.domain.convert.RadioSubjectConverter;
import com.feiclub.subject.domain.entity.SubjectAnswerBO;
import com.feiclub.subject.domain.entity.SubjectInfoBO;
import com.feiclub.subject.domain.entity.SubjectOptionBO;
import com.feiclub.subject.infra.basic.entity.SubjectRadio;
import com.feiclub.subject.infra.basic.service.SubjectRadioService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选题目的策略类
 *
 * @author: ChickenWing
 * @date: 2023/10/5
 */
@Component
@RequiredArgsConstructor
public class RadioTypeHandler implements SubjectTypeHandler {


    private final SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //单选题目的插入
        List<SubjectRadio> subjectRadioList = new LinkedList<>();
//        getOptionList   答案选项  List<SubjectAnswerBO>
        subjectInfoBO.getOptionList().forEach(option -> {
            SubjectRadio subjectRadio =
                    RadioSubjectConverter.INSTANCE.convertBoToEntity(option);
//            id全部一样    根据答案选项标识private Integer optionType;答案private String optionContent;
//            ;是否正确private Integer isCorrect分成多个对象  批量添加
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectRadioList.add(subjectRadio);
        });
        subjectRadioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(Long.valueOf(subjectId));
        //        根据条件查询表   在这里对象只有id
        List<SubjectRadio> result =
                subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOList =
                RadioSubjectConverter.INSTANCE.convertEntityToBoList(result);

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }

}
